package com.ubing.boot.notesforjava.test.NIO;

import com.ubing.boot.notesforjava.domel.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.time.LocalDateTime;

/**
 * @ClassName : test_common
 * @Description : 公共方法，减少重复代码的编写
 * @Author : ubing
 * @Date: 2023-03-25 10:24
 */
@Component
public class test_common {

    @Autowired
    RedisTemplate redisTemplate;
    private class com{
        public com(){
            UserInfo user = (UserInfo) redisTemplate.opsForValue().get("user");
            if(user!=null){
                this.createdUser= user.getUserName();
                this.userName= user.getUserName();
            }else {
                this.createdUser= "测试";
                this.userName= "测试";
            }
            this.createTime=LocalDateTime.now();
            this.updateTime=LocalDateTime.now();
            this.userStat=1;
        }
        private LocalDateTime createTime;
        private String userName;
        private LocalDateTime updateTime;
        private Integer userStat;
        private String createdUser;
    }

    public void setv(Object obj){
        com com = new com();
        Class<?> aClass = obj.getClass();
        Field[] declaredFields = com.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            String name = field.getName();
                try {
                    Field field1 = aClass.getDeclaredField(name);
                    if(field1!=null){
                        field1.setAccessible(true);
                        field1.set(obj,field.get(com));
                    }
                } catch (Exception e) {
                    continue;
                }
        }
    }
}
